﻿using System.Net.Mail;
using System.Web.Mvc;

namespace CoVanHocTapProject.Email
{
    /// <summary>
    /// Service to send out the emails. It doesn't use DI for 
    /// EmailTemplateService and SmtpClient, easy to do if needed.
    /// </summary>
    public class EmailService : IEmailService
    {
        private readonly EmailTemplateService emailService = new EmailTemplateService();
        private static readonly string viewPath = "~/Views/Emails/{0}.aspx";

        /// <summary>
        /// Takes a view, creates an email from it and sends it out.
        /// </summary>
        /// <param name="context">Context to render the view.</param>
        /// <param name="viewName">View that eventually becomes an email.</param>
        public void SendEmail(ControllerContext context, string viewName, object model) 
        {
            context.Controller.ViewData.Model = model;
            var email = emailService.RenderMessage(context, string.Format(viewPath, viewName));
            email.BodyEncoding = System.Text.Encoding.UTF8;

            new SmtpClient().Send(email);
        }
    }
}
